//
//  MYY_LoginViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/10/26.
//  Copyright © 2017年 hbjApple. All rights reserved.
// 登录

#import "MYY_LoginViewController.h"
#import "MYY_LoginView.h"
#import "MYY_User.h"
#import "NJXTLoginHelper.h"
#import "MYY_registerViewController.h"
#import "MYXBaseViewController.h"

@interface MYY_LoginViewController ()
@property(nonatomic, strong) MYY_LoginView *loginView;
@property(nonatomic, strong) MYXHttpManager *MYXHttpMana;
@end

@implementation MYY_LoginViewController

-(void)leftButt
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(WIDTH-50, 44, 30, 30);
    [btn setImage:[UIImage imageNamed:@"guanbi-2"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(exit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
  //  [self.navigationItem setRightBarButtonItem:cancelItem animated:NO];
    
}
-(void)exit
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.MYXHttpMana =[[MYXHttpManager alloc]init];
    self.navigationItem.title = @"登录";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.loginView];
    [self setupData];
}

- (void)setupData
{
    WS(weakSelf)
    NSArray *userList = [[NJXTLoginHelper helper] listAllUsers];
    [self.loginView setUserList:userList loginBlock:^(NSString *account, NSString *password) {
        [weakSelf.loginView updateEnableState:NO hasLogin:YES];
        [weakSelf.MYXHttpMana login:account
                               code:@""
                         Invitation:@""
                           password:password
                            sex:@"0"
                            success:^(MYY_User *user) {
            user.isLogin = YES;
            [user save];
//            NSString *account = [MYY_User readUserFromFile].accid;
//            NSString *token   = [MYY_User readUserFromFile].imtoken;
            if (user.token.length > 0) {
                [weakSelf.MYXHttpMana userinfo:^(MYXMemberMdel *user) {
                    if (user.userinfo.userid.length > 0) {
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"info" object:nil];
                        // 进入首页
                        [weakSelf dismissViewControllerAnimated:YES completion:nil];
                    }
                } failure:^(NSString *errorCode) {
                    
                } netDown:^{
                    
                }];
            }
                                NSLog(@"token--%@,userID--%@",user.token,user.uid);
//FIXME:- 网易云登录
//            [[[NIMSDK sharedSDK] loginManager] login:account
//                                               token:token
//                                          completion:^(NSError *error) {
//                                              NSLog(@"网易云---登录---error %@",error);
//
//                                          }];
//
//            BOOL isLogin = [[NIMSDK sharedSDK] loginManager].isLogined;
//            if (!isLogin) {
//
//            }

          //  [UIApplication sharedApplication].keyWindow.rootViewController = [[MYXBaseViewController alloc] init];
            
        }failure:^(NSString *errorCode) {
            
             [JXTHUDHelper showHUDText:errorCode type:@"error" inView:nil];
        } netDown:^{
             [JXTHUDHelper showHUDText:@"网络出错" type:@"error" inView:nil];
        }];
        
    }];
    
   
     [self leftButt];
}

#pragma mark - property
- (MYY_LoginView *)loginView
{
    if (_loginView == nil) {
        _loginView = [[MYY_LoginView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        _loginView.backgroundColor =MYYColorFromRGB(0x8fdcd4);
    }
    return _loginView;
}

//MYY_registerViewController
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
